/**
 * 修改TextureMerger生成的.json图集配置文件的二级key名,重命名追加同名前缀, 避免与其他图集冲突
 * @type {module:fs}
 */


var fs = require('fs');




function renameSubKey(fromFile) {

    console.log("[INFO] fromFile: ", fromFile);
    var fileName = fromFile.substring(fromFile.lastIndexOf("\\") + 1, fromFile.lastIndexOf(".json"));
    console.log("[INFO] fileName: ", fileName);
    var buf = fs.readFileSync(fromFile, "UTF-8");
    var data = JSON.parse(buf);
    var frames = data.frames;
    for(var key in frames){
        var rename = fileName+"_"+key;
        var value = frames[key];
        delete frames[key];
        frames[rename] = value;
    }
    fs.writeFileSync(fromFile, JSON.stringify(data), "UTF-8");

}

var fromDir = "D:\\User\\Documents\\PK\\clientH5\\resource\\mir2\\Item\\";
var files = fs.readdirSync(fromDir).filter(function (s) {
        return s.lastIndexOf(".json") > 0;
    }
)

for (var i in files) {
    try {
        renameSubKey(fromDir + files[i]);
    } catch (e) {
        console.log("[ERR ]  " + e);
    }

}


